//
//  WKMyWalletViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/1.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKMyWalletViewController.h"

@interface WKMyWalletViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WKMyWalletHeaderView *headerView;//头部view
@property (nonatomic, strong) WKMyWalletFooterView *footerView;//尾部view
@property (nonatomic, strong) NSArray *dataArray;//tableView数据源

@end

@implementation WKMyWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    [self.tableView registerNib:[UINib nibWithNibName:@"WKMyWalletTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyWalletCell"];
    self.dataArray = @[@"修改密码",@"导出私钥",@"导出Keystore"];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"我的%@钱包",self.walletModel.coinType];
    self.navigationView.topRightButton.hidden = NO;
    [self.navigationView.topRightButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.navigationView.topRightButton setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
    self.headerView.walletModel = self.walletModel;
    
    __weak typeof(self) weakSelf = self;
    weakSelf.footerView.deleteBlock = ^(){
        [weakSelf deleteWalletNetworking];
    };
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKMyWalletTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyWalletCell" forIndexPath:indexPath];
    cell.label.text = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        WKChangePasswordViewController *changePassword = [[WKChangePasswordViewController alloc] init];
        [self.navigationController pushViewController:changePassword animated:YES];
    }
    else if (indexPath.row == 1) {
        [self clickExportKey];
    }
    else if (indexPath.row == 2) {
        WKExportKeystoreViewController *exportKeystore = [[WKExportKeystoreViewController alloc] init];
        [self.navigationController pushViewController:exportKeystore animated:YES];
    }
}

//导出私钥
- (void)clickExportKey {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.4;
    [window.rootViewController.view addSubview: backView];
    
    WKExportKeyView *exportKeyView = [[[NSBundle mainBundle]loadNibNamed:@"WKExportKeyView" owner:nil options:nil] lastObject];
    exportKeyView.frame = [UIScreen mainScreen].bounds;
    [window.rootViewController.view addSubview:exportKeyView];
    
    exportKeyView.sureBlock = ^(NSString *textString){
        DLog(@"%@",textString);
        [backView removeFromSuperview];
    };
    exportKeyView.cancelBlock = ^(){
        [backView removeFromSuperview];
    };
}

//删除钱包
- (void)deleteWalletNetworking {
    [httpManager deleteWalletWithWalletId:self.walletModel.id success:^(NSDictionary *result) {
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (WKMyWalletHeaderView *)headerView {
    if (_headerView == nil) {
        _headerView = [[WKMyWalletHeaderView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 200)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}

- (WKMyWalletFooterView *)footerView {
    if (_footerView == nil) {
        _footerView = [[WKMyWalletFooterView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 200)];
        _footerView.backgroundColor = [UIColor whiteColor];
    }
    return _footerView;
}

@end
